<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <!-- 面包屑路径导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item to="/">首页</el-breadcrumb-item>
          <el-breadcrumb-item>素材管理</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- /面包屑路径导航 -->
      </div>
      <el-table :data="articles" style="width: 100%">
        <el-table-column prop="title" label="标题" width="180">
        </el-table-column>
        <el-table-column prop="total_comment_count" label="总评论数">
        </el-table-column>
        <el-table-column prop="fans_comment_count" label="粉丝评论数据">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            {{ scope.row.comment_status ? "正常" : "关闭" }}
          </template>
        </el-table-column>
        <el-table-column label="评论状态" width="180">
          <template slot-scope="scope">
            <!-- 开关组件绑定的数据是一个布尔值，它会根据布尔值的真假来决定开关状态 -->
            <el-switch
              :disabled="scope.row.statusDisabled"
              v-model="scope.row.comment_status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="onStatusChange(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-size.sync="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        background
        :total="totalCount"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getArticles, updateCommentStatus } from "@/api/article";
export default {
  // 组件的 name 最好起名为两个单词，尽量少用一个单词
  // 为什么？为了避免和原生的 html 标签冲突
  name: "CommentIndex",
  components: {},
  props: {},
  data() {
    return {
      tableData: [
        {
          date: "2016-05-02",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-04",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1517 弄",
        },
        {
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1519 弄",
        },
        {
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1516 弄",
        },
      ],
      articles: [],
      currentPage: 1,
      totalCount:0,
      pageSize:10
    };
  },
  computed: {},
  watch: {},
  created() {
    this.loadArticle(1);
  },
  methods: {
    //  获取文章
    loadArticle(page = 1) {
      // 每次选择 多少条/页的时候 让他回到第一页
      this.currentPage=page
      getArticles({
        response_type: "comment",
        page,
        per_page:this.pageSize
      }).then((res) => {
        const { results } = res.data.data;
        //手动增加 一个属性
        results.forEach((article) => {
          article.statusDisabled = false;
        });
        this.articles = res.data.data.results;
        this.totalCount = res.data.data.total_count
      });
    },

    handleSizeChange() {
      this.loadArticle(1)
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.loadArticle(val)
    },
    onStatusChange(article) {
      //   发送请求的时候开启禁用
      article.statusDisabled = true;
      updateCommentStatus(article.id.toString(), article.comment_status).then(
        (res) => {
          console.log(res);
          //完成之后关闭  //没完成则一直在关闭状态
          article.statusDisabled = false;
        }
      );
    },
  },
};
</script>

<style scoped>
.table-list {
  margin-bottom: 40px;
}
</style>